var sms_code =new Object();
$("#sms_code").click(function(){
	var phone = $("#phone").attr("value")
	if(phone==""){
		alert('请填写手机号码');
		return;
	}
	if(!phone.match(/^1[3|4|5|8][0-9]\d{4,8}$/)||phone.length!=11){
		alert('手机格式不正确,请重新输入');
		return;
	}		
	$.ajax({
		 url:"user_sendCode.html",
		 data:"phone="+phone,
		 dataType: 'json',
		 async:false,
		 success:sms_code.response
	});
})

var timer=new Object();
sms_code.response = function(res){
	if(res.success==true){
		timer.maxtime = 60 
		timer.res= setInterval("CountDown()",1000); 
		alert('发送成功,请注意查收');
	}else{
		if(res.success='fail_duplicate'){
			alert('该手机号已被他人使用,请更换手机号码');	
		}else{
			alert('发送失败,请重新发送');
		}
	}
}

var sms_verify = new Object();
$("#code").change(function(){
	var code = $("#code").val();
	var phone = $("#phone").val();
	if(phone==""){
		alert('请填写手机号');
		return;
	}
	if(!phone.match(/^1[3|4|5|8][0-9]\d{4,8}$/)||phone.length!=11){
		alert('手机格式不正确,请重新输入');
		return;
	}	
	if(code==""){
		alert('请填写验证码');
		return;
	}
	$.ajax({
		 url:"user_subverifyCode.htm",
		 data:"code="+code+"&phone="+phone,
		 dataType: 'json',
		 async:false,
		 success:sms_verify.response
	});
})

sms_verify.response = function(res){
	if(res.success==true){
		alert('验证成功,祝你学习成功');
		$("#phone").attr("readonly",true);
		$("#CODE").css('display','none');
	}else{
		if(res.success=='fail_duplicate'){
			alert('该手机号已被他人使用,请更换手机号码');
		}
		if(res.code==20){
			alert('验证码错误');
		}
		if(res.code==21){
			alert('验证手机和接收验证码手机非同一手机');
		}
	}
}

function CountDown()
{   
	if(timer.maxtime>0)
	{   
		//minutes = Math.floor(timer.maxtime/60);   
		//seconds = Math.floor(timer.maxtime%60);
		//document.all["timer"].innerHTML=msg;   
		--timer.maxtime;  
		countNumber = timer.maxtime;
		if(countNumber>0){
			msg = "再次发送("+timer.maxtime+" 秒)";
		}else{
			msg = "点击获取验证码";
		}		
		if(timer.maxtime==0){
			$("#sms_code").attr("value",msg);   
			$("#sms_code").removeAttr("disabled");
		}else{
			$("#sms_code").attr("value",msg);   
			$("#sms_code").attr("disabled","disabled");
		}
	}   
	else
	{
		clearInterval(timer.res);   
	}   
}   

